package com.zzjr.tools.java.ssm.factory;

import com.zzjr.tools.java.ssm.constants.AutoCodeConstants;
import com.zzjr.tools.java.ssm.utils.CamelUtils;
import com.zzjr.tools.java.ssm.utils.IOUtils;
import com.zzjr.tools.java.ssm.utils.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description: 用于生成Service实现类的工厂类
 * 
 * @Date Create on 2015年10月13日
 * @author <a href="mailto:Aromarich@zuozh.com">Aromarich</a>
 * @since version1.0 Copyright 2015 ZZJR All Rights Reserved.
 */
public class ServiceImplFactory {
	
	// Java Service实现类文件路径
	static private File serviceImplFile;
	
	// java model对象名
	static private String modelName;
	
	// 日期格式转换工具类
	static private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 创建Service接口文件
	 */
	public static void generateServiceImpl(String tbName) {
		System.out.println("开始生成Service实现类文件");
		// tableName = tbName;
		modelName = StringUtils.upperFirstChar(CamelUtils.convertUnderLineToCamel(tbName));
		
		// 1.创建service包
		String serviceImplDir = AutoCodeConstants.PACKAGE_FULL_PATH + File.separator + "biz" + File.separator
				+ AutoCodeConstants.SERVICE_SUFFIX.toLowerCase() + File.separator + "impl";
		IOUtils.mkDir(new File(serviceImplDir));
		
		// 2.创建文件
		String filePath = serviceImplDir + File.separator + modelName + AutoCodeConstants.SERVICE_SUFFIX + "Impl.java";
		serviceImplFile = new File(filePath);
		IOUtils.createNewFile(serviceImplFile);
		
		// 3.往Java文件中写入内容
		writeToServiceFile();
		System.out.println("完成Service实现类文件的生成");
	}
	
	/**
	 * 创建Service接口文件
	 */
	private static void writeToServiceFile() {
		// 2.生成Service接口主体代码
		StringBuilder content = parseServiceFile();
		FileWriter fw;
		PrintWriter pw;
		try {
			fw = new FileWriter(serviceImplFile);
			pw = new PrintWriter(fw);
			pw.println(content.toString());
			pw.flush();
			pw.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 生成Service接口文件的主方法
	 * 
	 * @return
	 */
	private static StringBuilder parseServiceFile() {
		StringBuilder sb = new StringBuilder();
		
		sb.append("package " + AutoCodeConstants.PACKAGE_NAME + ".biz."
				+ AutoCodeConstants.SERVICE_SUFFIX.toLowerCase() + ".impl;\r\n\r\n");
		sb.append("import java.util.List;\r\n\r\n");
		
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
		sb.append("import org.springframework.stereotype.Service;\r\n");
		
		
		sb.append("import com.zzjr.logging.LogManager;\r\n");
		sb.append("import com.zzjr.logging.Logger;\r\n");
		
		sb.append("import " + AutoCodeConstants.PACKAGE_NAME + ".dataobject." + modelName + ";\r\n\r\n");
		sb.append("import " + AutoCodeConstants.PACKAGE_NAME + ".biz." + AutoCodeConstants.DAO_SUFFIX.toLowerCase()
				+ "." + modelName + AutoCodeConstants.DAO_SUFFIX + ";\r\n\r\n");
		sb.append("import " + AutoCodeConstants.PACKAGE_NAME + ".biz." + AutoCodeConstants.SERVICE_SUFFIX.toLowerCase()
				+ "." + modelName + AutoCodeConstants.SERVICE_SUFFIX + ";\r\n\r\n");
		
		sb.append("/**\r\n");
		sb.append(" * Description:" + modelName + AutoCodeConstants.SERVICE_SUFFIX + "实现类\r\n");
		sb.append(" *\r\n");
		sb.append(" * @Date Create on " + sdf.format(new Date()) + "\r\n");
		sb.append(" * @author <a href=\"mailto:" + AutoCodeConstants.AUTHOR_NAME + "@zuozh.com\">"
				+ AutoCodeConstants.AUTHOR_NAME + "</a>\r\n");
		sb.append(" * @since version1.0 Copyright 2018 ZZJR All Rights Reserved.\r\n");
		sb.append(" */\r\n");
		
		sb.append("@Service\r\n");
		sb.append("public class " + modelName + AutoCodeConstants.SERVICE_SUFFIX + "Impl implements " + modelName
				+ AutoCodeConstants.SERVICE_SUFFIX + " {\r\n\r\n");
		
		sb.append("\t//创建日志对象\r\n");
		sb.append("\tprivate static final Logger logger = LogManager.getLogger(" + modelName
				+ AutoCodeConstants.SERVICE_SUFFIX + "Impl.class);\r\n\r\n");
		sb.append("\t@Autowired\r\n");
		sb.append("\tprivate " + modelName + AutoCodeConstants.DAO_SUFFIX + " " + StringUtils.lowerFirstChar(modelName)
				+ AutoCodeConstants.DAO_SUFFIX + ";\r\n\r\n");
		
		// 处理方法
		processMethod(sb);
		
		sb.append("}\r\n");
		
		return sb;
	}
	
	/**
	 * 生成接口方法
	 */
	private static void processMethod(StringBuilder sb) {
		sb.append("\t\t\r\n");
		// 1.insertXxx
		sb.append("\t@Override\r\n");
		sb.append("\tpublic int insert" + modelName + "(" + modelName + " "
				+ CamelUtils.convertUnderLineToCamel(modelName) + "){\r\n");
		sb.append("\t\t// 1.进行参数业务校验\r\n");
		sb.append("\t\tif (null == " + StringUtils.lowerFirstChar(modelName) + ") {\r\n");
		sb.append("\t\t\tlogger.error(\"insert" + modelName + " fail ! Reason : " + modelName + " is  null\");\r\n");
		sb.append("\t\t\treturn -1;\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t\t// 返回结果集\r\n");
		sb.append("\t\treturn " + StringUtils.lowerFirstChar(modelName) + AutoCodeConstants.DAO_SUFFIX + ".insert"
				+ modelName + "(" + StringUtils.lowerFirstChar(modelName) + ");\r\n");
		sb.append("\t}\r\n\r\n");
		
		// 2.deleteXxxById
		//		sb.append("\t@Override\r\n");
		//		sb.append("\tpublic int delete" + modelName + "ById(Long id){\r\n");
		//		sb.append("\t\t// 1.进行参数业务校验\r\n");
		//		sb.append("\t\tif (id < 0) {\r\n");
		//		sb.append("\t\t\tlogger.error(\"delete" + modelName + "ById fail!Reason:id < 0!\");\r\n");
		//		sb.append("\t\t\treturn -1;\r\n");
		//		sb.append("\t\t}\r\n");
		//		sb.append("\t\t// 返回结果集\r\n");
		//		sb.append("\t\treturn " + StringUtils.lowerFirstChar(modelName) + AutoCodeConstants.DAO_SUFFIX + ".delete"
		//				+ modelName + "ById(id);\r\n");
		//		sb.append("\t}\r\n\r\n");
		
		// 3.updateXxxById
		sb.append("\t@Override\r\n");
		sb.append("\tpublic int update" + modelName + "ById(" + modelName + " "
				+ CamelUtils.convertUnderLineToCamel(modelName) + "){\r\n");
		sb.append("\t\t// 1.进行参数业务校验\r\n");
		sb.append("\t\tif (null == " + StringUtils.lowerFirstChar(modelName) + ") {\r\n");
		sb.append("\t\t\tlogger.error(\"update" + modelName + "ById fail ! Reason : " + modelName + " is  null\");\r\n");
		sb.append("\t\t\treturn -1;\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t\t// 返回结果集\r\n");
		sb.append("\t\treturn " + StringUtils.lowerFirstChar(modelName) + AutoCodeConstants.DAO_SUFFIX + ".update"
				+ modelName + "ById(" + StringUtils.lowerFirstChar(modelName) + ");\r\n");
		sb.append("\t}\r\n\r\n");
		
		// 4.selectXxxById
		sb.append("\t@Override\r\n");
		sb.append("\tpublic " + modelName + " select" + modelName + "ById(Long id){\r\n");
		sb.append("\t\t// 1.进行参数业务校验\r\n");
		sb.append("\t\tif (id < 0) {\r\n");
		sb.append("\t\t\tlogger.error(\"select" + modelName + "ById fail!Reason:id < 0!\");\r\n");
		sb.append("\t\t\treturn null;\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t\t// 返回结果集\r\n");
		sb.append("\t\treturn " + StringUtils.lowerFirstChar(modelName) + AutoCodeConstants.DAO_SUFFIX + ".select"
				+ modelName + "ById(id);\r\n");
		sb.append("\t}\r\n\r\n");
		
		// 5.selectXxxListByCond
		sb.append("\t@Override\r\n");
		sb.append("\tpublic List<" + modelName + "> select" + modelName + "ListByCond(" + modelName + " "
				+ CamelUtils.convertUnderLineToCamel(modelName) + "){\r\n");
		sb.append("\t\t// 返回结果集\r\n");
		sb.append("\t\treturn " + StringUtils.lowerFirstChar(modelName) + AutoCodeConstants.DAO_SUFFIX + ".select"
				+ modelName + "ListByCond(" + CamelUtils.convertUnderLineToCamel(modelName) + ");\r\n");
		sb.append("\t}\r\n\r\n");
		
		// 6.selectXxxCntByCond
		sb.append("\t@Override\r\n");
		sb.append("\tpublic Integer select" + modelName + "CntByCond(" + modelName + " "
				+ CamelUtils.convertUnderLineToCamel(modelName) + "){\r\n");
		sb.append("\t\t// 返回结果集\r\n");
		sb.append("\t\treturn " + StringUtils.lowerFirstChar(modelName) + AutoCodeConstants.DAO_SUFFIX + ".select"
				+ modelName + "CntByCond(" + CamelUtils.convertUnderLineToCamel(modelName) + ");\r\n");
		sb.append("\t}\r\n\r\n");
	}
}
